package threadPoolTest;

import java.util.Arrays;
import java.util.concurrent.*;

/**
 * @program: javase
 * @description: 线程池
 * @author: lld
 * @create: 2021-03-20 16:19
 **/
public class Demo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        System.out.println(Runtime.getRuntime().availableProcessors());
        //不推荐使用 Executors 创建线程池
//        ExecutorService executor = Executors.newFixedThreadPool(10);
//        ExecutorService executor = Executors.newSingleThreadExecutor();
        ExecutorService executor = Executors.newCachedThreadPool();
        //使用 new ThreadPoolExecutor
//        ThreadPoolExecutor executor =null;
        try {
//            System.out.println(Integer.MAX_VALUE);
//            executor = new ThreadPoolExecutor(10, 10, 5L,
//                    TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
            for (int i = 0; i < 100; i++) {
                executor.execute(new MyThread());
            }
//        submit有Future返回值
            Future<?> submit = executor.submit(new MyThread());
            System.out.println(submit.get());

        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } finally {
//        必须关闭
            executor.shutdown();
        }

    }
}

class MyThread implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "ok");
    }
}